﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:57
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  ActionInRoleRepository : Repository<ActionInRole, ActionInRole, TbActionInRole>, IActionInRoleRepository<ActionInRole>
    {
        public override ActionInRole CreateEntity()
        {
            return new ActionInRole { };
        }
        public override IQueryable<ActionInRole> Get()
        {
            return MapQueryable(Entities.TbActionInRole);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbActionInRole).Count();
        }
        public override IQueryable<ActionInRole> Get(Expression<Func<ActionInRole, bool>> expression)
        {
            return MapQueryable(Entities.TbActionInRole).Where(expression);
        }
        public override IQueryable<ActionInRole> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbActionInRole), sort, order);
        }
        public override IQueryable<ActionInRole> GetSort(Expression<Func<ActionInRole, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbActionInRole).Where(expression), sort, order);
        }
        public override void Add(ActionInRole t)
        {
            Entities.AddToTbActionInRole(ReverseMapModel(t));
        }
        public override void Update(ActionInRole t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(ActionInRole t)
        {
            if (Entities.TbActionInRole.Any(o => o.CoActionInRoleId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbActionInRole(ReverseMapModel(t));
            }
        }
        public override void Delete(ActionInRole t)
        {
            Entities.TbActionInRole.Where(o => o.CoActionInRoleId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<ActionInRole, bool>> expression)
        {
            MapQueryable(Entities.TbActionInRole).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<ActionInRole> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<ActionInRole>();
        }
      
        public override sealed IQueryable<ActionInRole> MapQueryable(IQueryable<TbActionInRole> queryable)
        {
            return queryable.Select(o => new ActionInRole 
            {
         
	           Id=o.CoActionInRoleId,
	           ActionId = o.CoActionId 
,
	           RoleId = o.CoRoleId 
            
            });
        }
      
        public override sealed IQueryable<TbActionInRole> ReverseMapQueryable(IQueryable<ActionInRole> queryable)
        {
            return queryable.Select(o => new TbActionInRole 
            { 
        
	           CoActionInRoleId=o.Id,
	           CoActionId = o.ActionId
,
	           CoRoleId = o.RoleId
  
            
            });
        }
      
        public override sealed ActionInRole MapModel(TbActionInRole model)
        {
            return new ActionInRole {
	           Id=model.CoActionInRoleId,
	           ActionId = model.CoActionId
,
	           RoleId = model.CoRoleId
            };
        }
        
        public override sealed TbActionInRole ReverseMapModel(ActionInRole model)
        {
            return new TbActionInRole { 
	           CoActionInRoleId=model.Id,
	           CoActionId = model.ActionId
	        
,
	           CoRoleId = model.RoleId
	        
            
            
            };
        }


    }
}



